#!/bin/bash

# 更新历史记录：
# 20241005： 增加读出速度的选择功能，使用方法如下：
#          ./power_up不带参数时，默认以200kHz读出速度进行配置
#          ./power_up 500k，以500kHz读出速度进行配置
#          ./power_up 1M，以1MHz读出速度进行配置

if [ $# -eq 0 ]; then
    echo 'params_200kHz_44s' > .WKDIR
elif [ $# -eq 1 ]; then
    if [ $1 = '500k' ]; then
        echo 'params_500kHz_44s' > .WKDIR
    elif [ $1 = '1M' ]; then
        echo 'params_1MHz_44s' > .WKDIR
    else
        echo '##################################'
        echo '    option must be 500k or 1M'
        echo '##################################'
    fi
fi

CFG_DIR=`cat .WKDIR`

#echo $CFG_DIR
#exit

POWER_STATUS=`cat .power_status`

clear;

if [ $POWER_STATUS -eq 0 ]; then
    echo "***********************************************************"
    echo "> Camera has not been powered up"
    echo "> It's safe to power up the camera"
elif [ $POWER_STATUS -eq 1 ]; then
    echo "***********************************************************"
    echo "> Camera has been powered up, exit."
    echo "> (If you want to power up the camera, power down it fist)"
    exit;
fi

echo ""
echo "> *************************************************************"
echo "> powering up ..."
echo "> using configuration data from dir: ${CFG_DIR}"
echo "> *************************************************************"
echo ""

# AD9508
bin/ad9508_cfg ${CFG_DIR}/ad9508.ini
sleep 1

#ADC2REG_CLK
bin/adc2reg_clk -c  ${CFG_DIR}/adc2reg.ini
sleep 1

bin/ads5294_reset
sleep 1

bin/ads5294_cfg ${CFG_DIR}/ads5294.ini
sleep 1

# CCD drive parameters
bin/ccd290_drive_config ${CFG_DIR}/ccd_drv.ini
sleep 1

# DCDC weights
bin/dcds_w_cfg ${CFG_DIR}/dcds_weights_cfg.ini
sleep 1

# Daisy Chain
bin/daisy_cfg ${CFG_DIR}/daisy.ini 1

sleep 1
# DACx0508
bin/dacx0508_cfg ${CFG_DIR}/dacx0508.ini 1
sleep 1

# enable 156MHz clock 
bin/en_156M_clk 1
sleep 1

# AFE
bin/AFE_PW 1

#bin/debug_ccd_drv bias_ctr 0    # 临时将bias_ctr一直设为1
#sleep 1

bin/vtrans_cfg 0

bin/ads5294_sync

echo ""
echo "> ***************************"
echo "> powering up finished."
echo "> ***************************"
echo ""

# update power status, DON'T remove it
echo 1 > .power_status
